package com.ymatou.iapi.sellerproduct.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;

import com.ymt.core.tool.Logger;
import com.ymttest.business.YmatouAutoTestCall;
import com.ymttest.business.service.AssertService;
import com.ymttest.database.mongo.LiveProductInfoWapper;
import com.ymttest.database.mongo.LiveProductModelWapper;
import com.ymttest.database.mongo.ProductBasicInfoWapper;
import com.ymttest.database.mongo.ProductDetailModelWapper;
import com.ymttest.database.mongo.ProductListingModelWapper;
import com.ymttest.database.mongo.YmtMongoDriverFactroy;
import com.ymttest.database.sqlwapper.YmtProductsIWapper;
import com.ymttest.database.sqlwapper.YmtProductsInLiveWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.YMTDateUtil;

public class BatchSetProductOffSaleCall extends YmatouAutoTestCall{
	private static String opurl ="/api/Product/BatchSetProductOffSale";
	
	public BatchSetProductOffSaleCall() {
		super(EnvSetup.getSystemUrl("sellerproduct_iapi"),opurl,"POST","JSON");
	}
	@Override
	public void addDefHeader() {
		client.addHeader("Accept","text/xml,text/javascript,text/html,application/json");
		client.addHeader("Content-Type", "application/json; charset=UTF-8");
	}
	
	
	
	
	
	
	
	
	
	/**
	 * 下架商品公用方法
	 * @param productId
	 * @param sellerId
	 * @throws Exception
	 */
	public void batchSetProductOffSale(String productId,int sellerId) throws Exception {
	    try {
			List<String> productidList=new ArrayList<>();
			productidList.add(productId);
			Map<String,Object> requestMap = new HashMap<String, Object>();		    
			requestMap.put("ProductIds", new JSONArray(productidList));	
			requestMap.put("UserId",sellerId);			
			JSONObject result = new BatchSetProductOffSaleCall().callAndGetReturnData(requestMap);
			
	    }catch (Exception e) {
	        e.printStackTrace();
	    }
	}
	
	
	
	
	
	
	/**
	 * 接口返回信息公用信息部分断言（正向Case场景）
	 * @param dataStr 传入报文信息
	 * @throws Exception 错误信息
	 */
	public void batchSetProductOffSalereturncode(JSONObject dataStr,String productID1) throws Exception {
	    try {
			//返回报文断言
			String code =  dataStr.getString("Code");
			Logger.verifyIsContains(200, code, "\"Code\":200");
			String bCode =  dataStr.getString("BCode");
			Logger.verifyIsContains(0, bCode, "\"BCode\":0");
			String msg =  dataStr.getString("Msg");
			Logger.verifyIsContains("", msg, "Msg操作成功");
			//String trace =  dataStr.getString("Trace");
			//Logger.verifyIsNull(trace, "Trace等于空");			

	    }catch (Exception e) {
	        e.printStackTrace();
	    }
	}
	
	
	/**
	 * 下架商品成功场景的断言
	 * @param startTime	商品销售开始世界期待值
	 * @param productidList 下架商品list
	 * @throws Exception
	 */
	public void setOffSalecheck(String startTime , List<String> productidList) throws Exception {
	    try {

			for (String productid : productidList) {
				//SQL YmtProducts
				List<Map> productIDsqllist = new YmtProductsIWapper().selectProductByProductIdforMap(productid);
				String svalidStartbysql = (new SimpleDateFormat("yyyy-MM-dd")).format(productIDsqllist.get(0).get("validStart"));			
				String svalidEndbysql = (new SimpleDateFormat("yyyy-MM-dd")).format(productIDsqllist.get(0).get("validEnd"));						
				Logger.verifyEquals(startTime, svalidStartbysql, "商品开始销售时间");
				Logger.verifyEquals(startTime, svalidEndbysql, "商品结束销售时间");				
				
				if (YmtMongoDriverFactroy.isOldMongoCheck()){
					//Mango ProductBasicInfo
					Map<String, Object> productBasicInfo = new ProductBasicInfoWapper().getByProductId(productid); 		
					String validStartbyMango = (new SimpleDateFormat("yyyy-MM-dd")).format(productBasicInfo.get("ValidStart"));	
					String ValidEndbyMango = (new SimpleDateFormat("yyyy-MM-dd")).format(productBasicInfo.get("ValidEnd"));	
					Logger.verifyEquals(startTime, validStartbyMango, "商品开始销售时间");
					Logger.verifyEquals(startTime, ValidEndbyMango, "商品结束销售时间");
									
					//Mango LiveProductInfo
					Map<String, Object> liveProductInfo = new LiveProductInfoWapper().getliveproductByProductId(productid);
					if (liveProductInfo == null || liveProductInfo.size()<=0) {
							String liveProductInfo1 = "null";
							Logger.verifyEquals("null", liveProductInfo1, "如果商品曾加入直播，下架后，应删除Mango商品直播表");
						}else{
							Logger.verifyEquals("null", liveProductInfo, "如果商品曾加入直播，下架后，应删除Mango商品直播表");
					}
				}
				
				
				if (YmtMongoDriverFactroy.isNewMongoCheck()){
					//newMango ProductDetailModel
					Map<String,Object> productDetailModel = new ProductDetailModelWapper().getProductId(productid);
					AssertService.assertTime(YMTDateUtil.getDate(), (Date)productDetailModel.get("ValidStart"), 100);
					AssertService.assertTime(YMTDateUtil.getDate(), (Date)productDetailModel.get("ValidEnd"), 100);
													
					//newMango ProductListingModel
					Map<String,Object> productListingModel = new ProductListingModelWapper().getProductId(productid);
					AssertService.assertTime(YMTDateUtil.getDate(), (Date)productListingModel.get("ValidStart"), 100);
					AssertService.assertTime(YMTDateUtil.getDate(), (Date)productListingModel.get("ValidEnd"), 100);						
				}
								
			}

	    }catch (Exception e) {
	        e.printStackTrace();
	    }
	}
	
	
	/**
	 * 如果是直播商品，下架后直播表中应没有数据
	 * @param productidList
	 * @throws Exception
	 */
	public void setOffSaleforliveProduct(List<String> productidList) throws Exception {
	    try {
	
			for (String productid : productidList) {
					
				//SQL 商品直播表
				YmtProductsInLiveWapper pInLive = new YmtProductsInLiveWapper();	
				List<Map> pInLivelist = pInLive.selectInLiveByProductId(productid);
				if (pInLivelist == null || pInLivelist.size()<=0){
					Logger.verifyNotNull(pInLivelist, "Error：SQL商品直播表被删除了");
				}else{
					Logger.verifyEquals(-1, pInLivelist.get(0).get("iAction"), "SQL商品直播表 iaction 置为-1");
				}
				
				//newMango LiveProductModel
				Map<String, Object> LiveProductModel = new LiveProductModelWapper().getByProductId(productid);
				if (LiveProductModel == null || LiveProductModel.size()<=0) {
					String LiveProductModel1 = "null";
					Logger.verifyEquals("null", LiveProductModel1, "如果商品曾加入直播，下架后，应删除Mango商品直播表");
				}else{
					Logger.verifyEquals("null", LiveProductModel, "如果商品曾加入直播，下架后，应删除Mango商品直播表");					
				}
					
			}
								
	    }catch (Exception e) {
	        e.printStackTrace();
	    }
	}	
	
}
